package com.tepth.remote.control.delegate.apply.list;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.tepth.remote.control.R;
import com.tepth.remote.control.R2;
import com.tepth.remote.control.base.bean.ApplyHistoryBean;
import com.tepth.remote.control.base.config.ApiDictionary;
import com.tepth.remote.control.base.refresh.RefreshCallback;
import com.tepth.remote.control.base.refresh.RefreshHandler;
import com.tepth.remote.control.delegate.BasePermissionCheckerDelegate;
import com.tepth.remote.control.delegate.apply.examination.ExaminationDelegate;
import com.tepth.remote.core.net.CommonParams;
import com.tepth.remote.ui.alert.DialogLoader;
import com.tepth.remote.ui.alert.IDialogClickListener;
import com.tepth.remote.ui.recycler.MultipleFields;
import com.tepth.remote.ui.recycler.MultipleItemEntity;
import com.tepth.remote.ui.recycler.MultipleRecyclerAdapter;
import com.tepth.remote.ui.recycler.MultipleRecyclerClickListener;
import com.tepth.remote.ui.recycler.grid.GridItemType;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description:
 *
 * @author Hequn.Lee
 * @date 2018/11/23
 */
public class ApplyListDelegate extends BasePermissionCheckerDelegate implements MultipleRecyclerClickListener, RefreshCallback {

    @BindView(R2.id.icon_toolbar_title)
    AppCompatTextView toolbarTitle = null;
    @BindView(R2.id.multiple_recyclerView)
    RecyclerView mRecyclerView = null;
    @BindView(R2.id.multiple_swipeRefresh)
    SwipeRefreshLayout mSwipeRefreshLayout = null;
    private RefreshHandler mRefreshHandler = null;
    private DialogLoader mDialogLoader = null;
    private MultipleRecyclerAdapter mAdapter = null;

    @OnClick(R2.id.icon_toolbar_back)
    void onBack() {
        pop();
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_multiple_list;
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        toolbarTitle.setText("请假申请列表");
    }

    private void initRecyclerView() {
        mDialogLoader = new DialogLoader(getProxyActivity());
        final LinearLayoutManager manager = new LinearLayoutManager(getProxyActivity());
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration
                .Builder(getProxyActivity())
                .color(R.color.font_gray_light)
                .size(1)
                .build());
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        initRecyclerView();
        mDialogLoader.showProgressDialog(false, "正在加载...", "");
        mRefreshHandler = RefreshHandler.create(mSwipeRefreshLayout,
                mRecyclerView, new ApplyListDataConvert(),
                ApiDictionary.API_GET_APPLY_LIST, this,
                CommonParams.SUCCESS_CODE_NORMAL, 1);
        mRefreshHandler.firstLoadData(this);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        mRefreshHandler.firstLoadData(this);
    }

    @Override
    public void onclickListener(MultipleFields type, int id) {
        if (mAdapter == null) {
            mAdapter = mRefreshHandler.getAdapter();
        }
        final List<MultipleItemEntity> arrayList = mAdapter.getData();
        ApplyHistoryBean result = null;
        final int size = arrayList.size();
        for (int i = 0; i < size; i++) {
            final ApplyHistoryBean bean = arrayList.get(i).getField(type);
            if (bean.getAutoId() == id) {
                result = bean;
            }
        }
        if (result != null) {
            final Bundle bundle = new Bundle();
            bundle.putParcelable(ExaminationDelegate.APPLY, result);
            start(ExaminationDelegate.create(bundle));
        } else {
            mDialogLoader.showFailed("未找到该请假单", "", new IDialogClickListener() {
                @Override
                public void onClick() {
                    mDialogLoader.clearDialog();
                }
            });
        }
    }

    @Override
    public void onLoadRecyclerError(String msg) {
        mDialogLoader.showFailed("加载失败", "", new IDialogClickListener() {
                    @Override
                    public void onClick() {
                        mRefreshHandler.requestSoapData(false, ApplyListDelegate.this);
                    }
                },
                new IDialogClickListener() {
                    @Override
                    public void onClick() {
                        pop();
                    }
                });
    }

    @Override
    public void onclickViewPagerListener(GridItemType type) {
    }

    @Override
    public void onRefreshCallback() {
        mDialogLoader.clearDialog();
    }
}
